package com.base.server.sys.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.base.server.sys.dao.SysDictDao;
import com.base.server.sys.enums.SysDictTypeCode;
import com.base.server.sys.pojo.SysDict;
import com.base.server.sys.service.SysDictService;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameJsonUtil;
import com.system.comm.utils.FrameStringUtil;
import com.system.handle.model.ResponseFrame;

@Component
public class SysDictServiceImpl implements SysDictService {

	private static final Logger LOGGER = LoggerFactory.getLogger(SysDictServiceImpl.class);
	@Autowired
	private SysDictDao sysDictDao;

	@Override
	public SysDict get(String typeCode, String dictId) {
		LOGGER.info("get typeCode:" + typeCode + " - dictId:" + dictId);
		return sysDictDao.get(typeCode, dictId);
	}

	@Override
	public ResponseFrame saveOrUpdate(SysDict sysDict) {
		ResponseFrame frame = new ResponseFrame();
		LOGGER.info("saveOrUpdate sysDict:" + FrameJsonUtil.toString(sysDict));
		if (FrameStringUtil.isEmpty(sysDict.getDictId())) {
			frame.setCode(-2);
			frame.setMessage("请传入参数");
			return frame;
		}
		SysDict org = get(sysDict.getTypeCode(), sysDict.getDictId());
		if(org == null) {
			sysDictDao.save(sysDict);
		} else {
			sysDictDao.update(sysDict);
		}
		frame.setBody(sysDict);
		frame.setSucc();
		return frame;
	}

	@Override
	public ResponseFrame pageQuery(SysDict sysDict) {
		ResponseFrame frame = new ResponseFrame();
		sysDict.setDefPageSize();
		int total = sysDictDao.findSysDictCount(sysDict);
		List<SysDict> data = null;
		if(total > 0) {
			data = sysDictDao.findSysDict(sysDict);
		}
		Page<SysDict> page = new Page<SysDict>(sysDict.getPage(), sysDict.getSize(), total, data);
		frame.setBody(page);
		frame.setSucc();
		return frame;
	}

	@Override
	public ResponseFrame delete(String typeCode, String dictId) {
		ResponseFrame frame = new ResponseFrame();
		sysDictDao.delete(typeCode, dictId);
		frame.setSucc();
		return frame;
	}

	@Override
	public ResponseFrame deleteByTypeCode(String typeCode) {
		ResponseFrame frame = new ResponseFrame();
		sysDictDao.deleteByTypeCode(typeCode);
		frame.setSucc();
		return frame;
	}

	@Override
	public String getValue(String typeCode, String dictId) {
		SysDict dict = get(typeCode, dictId);
		return dict == null ? null : dict.getValue();
	}

	@Override
	public String getValue(SysDictTypeCode sysDictTypeCode, String dictId) {
		return getValue(sysDictTypeCode.getCode(), dictId);
	}

	@Override
	public List<SysDict> findByTypeCode(String typeCode) {
		return sysDictDao.findByTypeCode(typeCode);
	}
	
}